from PySide6.QtWidgets import QWidget, QPushButton
from ...Input import Input
from util.Mouse import MouseUtil


class TextContain(QWidget):
    def __init__(self, parent: QWidget):
        super(TextContain, self).__init__(parent)

        lineHeight = int(parent.height() / 2)
        # x
        self.inputX = Input(self, 'x', 6, 'number')
        self.inputX.text = 0
        self.inputX.setFixedHeight(lineHeight)
        self.inputX.setPos(0, 0)

        # y
        self.inputY = Input(self, 'y', 6, 'number')
        self.inputY.text = 0
        self.inputY.setFixedHeight(lineHeight)
        self.inputY.setPos(self.inputX.x() + self.inputX.width(), 0)

        # 宽
        self.inputWidth = Input(self, '宽', 6, 'number')
        self.inputWidth.text = 0
        self.inputWidth.setFixedHeight(lineHeight)
        self.inputWidth.setPos(self.inputY.x() + self.inputY.width(), 0)

        # 高
        self.inputHeight = Input(self, '高', 6, 'number')
        self.inputHeight.text = 0
        self.inputHeight.setFixedHeight(lineHeight)
        self.inputHeight.setPos(self.inputWidth.x() + self.inputWidth.width(), 0)

        # 文字
        self.inputText = Input(self, '文字', 16, 'string')
        self.inputText.setPos(0, lineHeight)

        self.btnTrack = QPushButton(self)
        self.btnTrack.setText('选择区域')
        self.btnTrack.clicked.connect(self.startClickListener)
        self.btnTrack.setGeometry(self.inputText.x() + self.inputText.width(), lineHeight, 80, lineHeight)

        self.setFixedSize(self.inputHeight.x() + self.inputHeight.width(), parent.height())

    def onMouseClick(self, x, y, width, height, button):
        self.inputX.text = x
        self.inputY.text = y
        self.inputWidth.text = width
        self.inputHeight.text = height

    def startClickListener(self):
        MouseUtil.listenArea(self.onMouseClick)

    @property
    def data(self):
        return {
            "x": int(self.inputX.text),
            "y": int(self.inputY.text),
            "width": int(self.inputWidth.text),
            "height": int(self.inputHeight.text),
            "text": self.inputText.text
        }

    @data.setter
    def data(self, data):
        self.setData(data)
        self.inputX.text = data['x']
        self.inputY.text = data['y']
        self.inputWidth.text = data['width']
        self.inputHeight.text = data['height']
        self.inputText.text = data['text']
